$(document).ready(function() {
    var VAL = {
        NORMAL : "0000000",
        EVT_USER     : $("#userId").text(),
        T_FDPAPLYRS  : "FDPAPLYRS",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {

        $rsvNo   	 : $("#rsvNo"),
        $rsvNoSel   : $("#rsvNoSel"),

        buttons : {
            $save_btn     	: $("#save_btn"),
            $cancel_btn   	: $("#cancel_btn"),
            $query_btn   	: $("#query_btn"),
        },
        grids : {
            $mtrlListDiv 	: $("#mtrlListDiv"),
            $mtrlListGrd	: $("#mtrlListGrd"),
            $mtrlListPg    	: $("#mtrlListPg"),
        },dialogs : {
            $queryModel 	: $("#queryModel"),
            $queryBtn    	: $("#queryBtn"),
        }
    };
    var iniGridInfo = function(){
        var mtrlListCM = [
            {name: 'rsv_no',         index: 'rsv_no',              label:RSV_NO_TAG,             width: 150},
            {name: 'item_no',        index: 'item_no',             label:ITEM_NO_TAG,            width: 150},
            {name: 'mtrl_prod_id',   index: 'mtrl_prod_id',       label:MTRL_ID_TAG,            width: 150},
            {name: 'mtrl_cate',      index: 'mtrl_cate',           label:MTRL_CATE_TAG,          width: 150},
            {name: 'mtrl_pln_qty',   index: 'mtrl_pln_qty',        label:MTRL_PLN_QTY_TAG,      width: 150},
            {name: 'mtrl_act_qty',   index: 'mtrl_act_qty',        label:MTRL_ACT_QTY_TAG,      width: 150},
            {name: 'mtrl_unit',      index: 'mtrl_unit',           label:MTRL_UNIT_TAG,          width: 150},
            {name: 'mtrl_lot_id',    index: 'mtrl_lot_id',         label:MTRL_LOT_ID_TAG,        width: 150},
            {name: 'fab_id',          index: 'fab_id',              label:OPE_FAB_ID_TAG,        width: 150},
            {name: 'source_shop',     index: 'source_shop',        label:SOURCE_SHOP_TAG,        width: 150},
            {name: 'dest_shop',       index: 'dest_shop',           label:DEST_SHOP_WH_TAG,        width: 150},
        ];
        domObj.grids.$mtrlListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            height: 400,
            autowidth: true,
            shrinkToFit:false,
            autoScroll: false,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            rowNum: 20,
            multiselect : false,
            emptyrecords: true,
            colModel: mtrlListCM,
            pager: "#mtrlListPg"
        });

    };
    var btnFunc = {
        saveFun: function () {
            var rsvNo = $.trim(domObj.$rsvNo.val());
            if (!rsvNo) {
                showErrorDialog("", "请选择预留进行确认");
                return false;
            }
            var inObj = {
                trx_id     : VAL.T_FDPAPLYRS,
                evt_usr    : VAL.EVT_USER ,
                action_flg : "S",
                type_id     :"S",
                rsv_no      : rsvNo
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("确认调拨成功");
                domObj.grids.$mtrlListGrd.jqGrid("clearGridData");
                initSelectFnc.iniRsvNo();
            }
        },
        cancelFun: function () {
            var rsvNo = $.trim(domObj.$rsvNo.val());
            if (!rsvNo) {
                showErrorDialog("", "请选择预留进行取消");
                return false;
            }
            var inObj = {
                trx_id     : VAL.T_FDPAPLYRS,
                evt_usr    : VAL.EVT_USER ,
                action_flg : "S",
                type_id     :"D",
                rsv_no      : rsvNo
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("调拨取消成功");
                domObj.grids.$mtrlListGrd.jqGrid("clearGridData");
                initSelectFnc.iniRsvNo();
            }
        },
        prdMtrlFun: function () {
            var rsvNo = $.trim(domObj.$rsvNo.val());
            var inObj = {
                trx_id : VAL.T_FDPAPLYRS,
                action_flg : "Q",
                type_id    : 'I',
                rsv_no : rsvNo
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, domObj.grids.$mtrlListGrd);
            }
        },
        queryFun: function () {
            initSelectFnc.iniRsvNo();
            domObj.grids.$mtrlListGrd.jqGrid('clearGridData');
            domObj.dialogs.$queryModel.modal({
                backdrop: true,
                keyboard: false,
                show: false
            });
            domObj.dialogs.$queryModel.modal("show");
        },
        prdFun: function () {
            var rsvNoSel = $.trim(domObj.$rsvNoSel.val());
            var inObj = {
                trx_id : VAL.T_FDPAPLYRS,
                action_flg : "Q",
                type_id    : 'O',
                rsv_no : rsvNoSel
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, domObj.grids.$mtrlListGrd);
                domObj.dialogs.$queryModel.modal("hide");
            }
        }
    };
    /**
     * Ini contorl's data
     */
    var initSelectFnc = {
        iniRsvNo :function(){
            var iary={}
            var inObj = {
                trx_id : VAL.T_FDPAPLYRS,
                action_flg : "R",
                type_id    : 'I',
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$rsvNo, outObj.oary, "rsv_no","rsv_no","",true);
            }
        },
        iniRsvNoSel :function(){
            var iary={}
            var inObj = {
                trx_id : VAL.T_FDPAPLYRS,
                action_flg : "R",
                type_id    : 'O',
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$rsvNoSel, outObj.oary, "rsv_no","rsv_no","",true);
            }
        }
    };
    var iniContorlData = function(){
        initSelectFnc.iniRsvNo();
        initSelectFnc.iniRsvNoSel();
    };

    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        domObj.buttons.$save_btn.click(function(){
            btnFunc.saveFun();
        });
        domObj.buttons.$cancel_btn.click(function(){
            btnFunc.cancelFun();
        });
        domObj.buttons.$query_btn.click(function(){
            btnFunc.queryFun();
        });
        domObj.dialogs.$queryBtn.click(function(){
            btnFunc.prdFun();
        });
        domObj.$rsvNo.change(function(){
            btnFunc.prdMtrlFun();
        });
    };

    /**
     * Other action bind
     */
    var otherActionBind = function(){

    };
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
        iniGridInfo();
        iniContorlData();
        iniButtonAction();
        otherActionBind();
    };

    initializationFunc();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth,offsetBottom1, divWidth1;
        divWidth = $("#mtrlListDiv").width();
        offsetBottom =  $(window).height() - $("#mtrlListDiv").offset().top;
        $("#mtrlListDiv").height(offsetBottom * 0.95);
        $("#mtrlListGrd").setGridWidth(divWidth * 0.98);
        $("#mtrlListGrd").setGridHeight(offsetBottom * 0.99 - 51);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});
